package pl.praca.magisterska.web;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import pl.praca.magisterska.model.Person;
import pl.praca.magisterska.service.PersonService;

@Controller
@SessionAttributes
public class PersonController {

	
	private static String NOK = "NOK";
	private static String OK = "OK";
	
	@Autowired
	private PersonService personService;
	
	private static Logger log = Logger.getLogger(PersonController.class);
	
	
	@RequestMapping(method=RequestMethod.POST, value="/loginPerson")
	public @ResponseBody String loginPerson(@RequestBody Person person) {
		log.info("loginPerson: "+person);
		Integer result = personService.checkPersonPassword(person);
		if((result==null)||(result.equals(0))){
			return "NOK";
		}else{
			return "OK,"+result.toString();
		}	
	}
	
	@RequestMapping(method=RequestMethod.POST, value="/registerPerson")
	public @ResponseBody String registerPerson(@RequestBody Person person) {
		log.info("registerPerson: "+person);
		Integer result = personService.insertPerson(person);
		if((result==null)||(result.equals(0))){
			return "NOK";
		}else{
			return "OK";
		}	
	}
		
	@RequestMapping(method=RequestMethod.GET, value="/getPersonById/{idPerson}")//, headers="Accept=application/json")
	public @ResponseBody Person getPersonById(@PathVariable Integer idPerson) {
		log.info("getPersonById: "+idPerson);
		Integer id = personService.selectIdPerson(idPerson);
		Person person = personService.selectPerson(idPerson);
	    return person;
	}
	
}
